<?php
namespace Mob \ Controller;

use Think \ Controller;
use Think \ Hook;

class RedPackController extends BaseController {

   private $WxUser;


	public function _initialize() {
		parent :: _initialize();
		$this->_top_menu_list = array (
			'left' => array (
				array (
					'type' => 'home',
					'href' => U('Mob/Task/index'
				)
			),
			array (
				'type' => 'message'
			),


		), 'center' => array (
			'title' => '任务'
		));
		if (is_login()) {
			if (check_auth('Weibo/Index/doSend')) {
				$this->_top_menu_list['right'][] = array (
					'type' => 'edit',
					'href' => U('Mob/Weibo/addWeibo'
				));
			} else {
				$this->_top_menu_list['right'][] = array (
					'type' => 'edit',
					'info' => '你没有发布任务权限！'
				);
			}
		} else {
			$this->_top_menu_list['right'][] = array (
				'type' => 'edit',
				'info' => '登录后才能操作！'
			);
		}
		//dump($this->_top_menu_list);exit;

		$this->UserInfo = query_user(array('nickname', 'avatar64', 'uid'), $v['uid']);
		//$this->WxUser = get_WxOpenId();
		$this->setMobTitle('任务');
		$this->assign('top_menu_list', $this->_top_menu_list);
	}

	 /**
	 * 主页面显示
	 */
	public function index() {


		$this->display();

	}

	public function openRedPack()
   {
   	 //parse_str($_GET['param'],$param);
   	 $paySerialno = I('redpackId');
   	 $businessId  = I('businessId');
   	 $uid = get_uid();
   	 $is_open=0;
   //	 $is_open=false ;


      if($uid!=0)
      {
       $redPackRecord = D('RedPackRecord');  //实例化红包日志模型
       $isApply=$redPackRecord->isApply($paySerialno,$uid);  //判断是否已经领取过了。

       if($isApply)  //如果没有领取过的话
       {
       $redpack = D('RedPack');
   	    $isAcquire = $redpack->isAcquire($paySerialno);  //判断当前是否还有红包

     if($isAcquire) //如果还有红包的话,
     {

      $isEligibility  = $redpack->isEligibility($paySerialno,$uid);
      if($isEligibility) //判断一下是否符合领取的条件
      {
       $serial= $redPackRecord->creationRecord($paySerialno,1,is_login(),$this->WxUser);  //创建领取记录

       $moeny= $redpack->openRedPack($paySerialno,$uid,get_WxOpenId(),$serial);
       $is_open=1;
       if($moeny)
       {
       $aRecPack = array('status'=>1,'moeny'=>number_format($moeny/100,2));



       }else
       {
      $aRecPack = array('status'=>0);
       }
      }
   	 }

      
       }
       

      }
      $rp = D('RedPack');
      $redpack = $rp->queryRedPack($paySerialno);
     // print_r(M()->getLastSql());
	 $redpack['amount']=number_format($redpack['amount']/100,2);

      $redpack['user']=query_user(array('nickname', 'avatar64',  'avatar32','uid'), $redpack['releaseUid']);
	  $redpack['ranking'] = D('RedPackRecord')->redPackRanking($paySerialno);
	 //

	  foreach($redpack['ranking'] as &$v)
	  {
	  $v['user']= query_user(array('nickname', 'avatar64',  'avatar32','uid'), $v['applyUid']);

	  }

	  $this->assign('is_open',$is_open);
      $this->assign('moeny',$aRecPack);
	  $this->assign('redpack',$redpack);
	  $this->display();


	}

	public function ranking()
	{
	 $paySerialno = I('redpackId','','op_t');
	 $rp = D('RedPack');

	 $redpack = $rp->queryRedPack($paySerialno);
	 $redpack['amount']=number_format($redpack['amount']/100,2);
   // print_r(M()->getLastSql());
      $redpack['user']=query_user(array('nickname', 'avatar64',  'avatar32','uid'), $redpack['releaseuid']);
	  $redpack['ranking'] = D('RedPackRecord')->redPackRanking($paySerialno);
	 // print_r(M()->getLastSql());
	  $count=1;

	  foreach($redpack['ranking'] as &$v)
	  {
	  $v['user']= query_user(array('nickname', 'avatar64',  'avatar32','uid'), $v['applyUid']);
	  $v['order']=$count;
	  $count++;

	  }
	  $this->assign('redpack',$redpack);
	 //print_r($redpack);
	  $this->display();


	}
	public function detailRecord()
	{
	 $paySerialno = I('redPackId','','op_t');
      $detail = D('RedPackRecord')->detailRecord($paySerialno);
	  foreach($detail as &$v)
	  {
	  $v['user']= query_user(array('nickname', 'avatar64',  'avatar32','uid'), $v['applyUid']);
	  }

     $this->assign('detail',$detail);
     $this->display();
     }


   public function openRedPack1()
   {
   	 //parse_str($_GET['param'],$param);
   	 $paySerialno = I('post.paySerialno');
   	 $businessId  = I('post.businessId');



   	 if(is_login()==0)
   	 {
   	$this->ajaxReturn(array('status'=>0,'info'=>'亲，请登录后再来抢红包哦。~'));

   	 }


   	  $redpack = D('RedPack');
   	 $isAcquire = $redpack->isAcquire($paySerialno);  //判断
   	 if(!$isAcquire)
   	 {
   	$this->ajaxReturn(array('status'=>0,'info'=>'手慢了，红包已经抢完了~'));
   	 }

     $redPackRecord = D('RedPackRecord');
     $isApply=$redPackRecord->isApply($paySerialno,is_login());  //判断是否已经领取过了。
     if(!$isApply)  ///已经领取过了
      {
     	$this->ajaxReturn(array('status'=>0,'info'=>'不能重复领取~'));
      }

    $isEligibility  = $redpack->isEligibility($paySerialno,is_login());

     if(!$isEligibility)  //不符合领取范围
     {
      $this->ajaxReturn(array('status'=>0,'info'=>'亲，你不符合领取条件哦~'));

     }
   $serial= $redPackRecord->creationRecord($paySerialno,1,is_login(),$this->WxUser);


   $moeny= $redpack->openRedPack($paySerialno,is_login(),get_WxOpenId(),$serial);

      if($moeny)
      {
      $data['status']=1;
      $data['moeny']=number_format($moeny/100,2);
      $data['info']="领取红包成功！";
      }else
      {
      $data['status']=0;
      $data['info']="手气欠佳！";
   //  $data['info']= M()->getLastSql();
      }
      $this->ajaxReturn($data);



	}








}



